import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/parts/api/v1';

const initDesc = formatMessage({
    id: 'api.message.initDesc',
    defaultMessage: '初始化界面'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询申报要素'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询申报要素详情'
});
const exportDesc = formatMessage({
    id: 'api.message.exportDesc',
    defaultMessage: '导出申报要素'
});
const newOrderDesc = formatMessage({
    id: 'api.message.newOrderDesc',
    defaultMessage: '新增申报要素'
});
const modifyDesc = formatMessage({
    id: 'api.message.modifyDesc',
    defaultMessage: '修改申报要素'
});
const abandonDataDesc = formatMessage({
    id: 'api.message.abandonDataDesc',
    defaultMessage: '作废申报要素'
});
const partQueryDesc = formatMessage({
    id: 'api.message.partQueryDesc',
    defaultMessage: '备件查询'
});
const hsCodeImportTempDesc = formatMessage({
    id: 'api.hsCodeImportTempDesc',
    defaultMessage: '下载导入申报要素模板'
});

//初始化
export const getInitData = () => request(`${baseUrl}/ui/hsCode/init`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, initDesc));
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/hsCodes/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/hsCodes/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
//新增
export const newOrder = data => request(`${baseUrl}/hsCodes`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newOrderDesc));
//修改
export const modifyOrder = (id, data) => request(`${baseUrl}/hsCodes/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyDesc));
//作废
export const abandonData = (id, data) => request(`${baseUrl}/hsCodes/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDataDesc));
// 导出
export const exportPartIns = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/hsCodes/export/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, exportDesc))
        .then(res => res.data);
};
//查询备件
export const partQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/parts/forHsCode?${queryString}`, null, dto.PartList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partQueryDesc));
};

export const hsCodesImportTemp = () => request(`${baseUrl}/hsCodes/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, hsCodeImportTempDesc)).then(res => res.data);
